use log::{Level, LevelFilter, Metadata, Record, error, info, warn};

struct CustomLogger;

impl log::Log for CustomLogger {
    fn enabled(&self, metadata: &Metadata) -> bool {
        metadata.level() <= Level::Trace
    }

    fn log(&self, record: &Record) {
        if self.enabled(record.metadata()) {
            let call_site = record.module_path().unwrap_or("unknown");
            let line = record.line().unwrap_or(0);
            let level = record.level();

            // 构建格式化的日志行
            let message = format!(
                "[{}] [{}] [{}] [{}] [{}]",
                chrono::Local::now().format("%Y-%m-%d %H:%M:%S%.3f"),
                call_site,
                level,
                record.args(),
                line
            );
            eprintln!("{}", message);
        }
    }

    fn flush(&self) {}
}

// 初始化日志系统
pub fn init_logger() {
    log::set_boxed_logger(Box::new(CustomLogger))
        .map(|()| log::set_max_level(LevelFilter::Info))
        .unwrap();
}